package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: ggm
 * @data 2024年11月05日 18:39
 */
@RestController
@Slf4j
@RequestMapping("/admin/category")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    /**
     * @Author: ggm
     * @date: 2024/11/5
     * 分类分页查询
     */
    @GetMapping("/page")
    public Result<PageResult> categoryPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        //1,打印日志
        log.info("分类分页查询:{}", categoryPageQueryDTO);
        //2,调用service层
        PageResult pageResult = categoryService.page(categoryPageQueryDTO);
        //3,返回结果
        return Result.success(pageResult);

    }

    /**
     * @Author: ggm
     * @date: 2024/11/5
     * 删除分类
     */
    @DeleteMapping
    public Result deleteCategoryById(Long id) {
        log.info("删除分类:{}", id);
        categoryService.deleteById(id);
        return Result.success();

    }

    /**
     * @Author: ggm
     * @date: 2024/11/5
     * 新增分类
     */
    @PostMapping
    public Result<String> addCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类:{}", categoryDTO);
        categoryService.add(categoryDTO);
        return Result.success();

    }

    /**
     * @Author: ggm
     * @date: 2024/11/6
     * 修改分类信息
     */
    @PutMapping
    public Result<String> updateCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类:{}", categoryDTO);
        categoryService.update(categoryDTO);

        return Result.success();

    }

    /**
     * @Author: ggm
     * @date: 2024/11/6
     * 启用禁用分类
     */
    @PostMapping("/status/{status}")
    public Result<String> statusCategory(@PathVariable("status") Integer status, Long id) {
        log.info("启用禁用分类:{},{}", status, id);
        categoryService.update(status, id);
        return Result.success();

    }

    /**
     * @Author: ggm
     * @date: 2024/11/6
     * 根据类型查询分类
     */
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type) {
        List<Category> list = categoryService.selectByType(type);
        return Result.success(list);
    }


}
